<?php
	class DT_cats_Widget extends WP_Widget {
		function DT_cats_Widget() {
			parent::WP_Widget( 'dt_categories', $name = THEME_TITLE. __( ' Categories', 'dt') );
		}

		function form($instance) {
			$defaults = array(
                'title'     => '',
                'cols'      => 1
            );
            $instance = wp_parse_args( $instance, $defaults );
            
            // outputs the options form on admin
            $title = esc_attr( $instance[ 'title' ] );
            $cols = intval( $instance[ 'cols' ] );
			?>
			<p>
				<label for="<?php echo $this->get_field_id('title'); ?>">
                    <?php _e( 'Title:', LANGUAGE_ZONE ); ?>
                </label>
				<input 	style="width: 100px;" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
			</p>
			<p>
				<label for="<?php echo $this->get_field_id('columns'); ?>">
                    <?php _e( 'Columns:', LANGUAGE_ZONE ) ?>
                </label>
				<select style="width: 100px;" id="<?php echo $this->get_field_id('columns'); ?>" name="<?php echo $this->get_field_name('cols'); ?>">
				   <option value="1"<?php selected($cols, 1); ?>>1</option>
				   <option value="2"<?php selected($cols, 2); ?>>2</option>
				</select>
			</p>
			<?php
		}

		function update($new_instance, $old_instance) {
			// processes widget options to be saved
			$instance = $old_instance;
			$instance['title'] = strip_tags($new_instance['title']);
			$instance['cols'] = intval($new_instance['cols']);
			return $instance;
		}

		function widget($args, $instance) {
			// outputs the content of the widget
			extract( $args );
			
			$title = apply_filters( 'widget_title', $instance['title'] );
			$cols = isset($instance['cols'])?$instance['cols']:1;
            $cols_class = '';
            $first_count = 1;
            
            if( 2 == $cols ) {
                $cols_class = ' cat_two_cols';
                $first_count = 2;
            }
            
			ob_start();
			wp_list_categories('orderby=name');
			$ret=ob_get_clean();
			preg_match_all('/(<a[^>]+>[^<]+<\/a>)/', $ret, $m);
			$cats_count = count( $m[1] );
			$i = 0;
			
			echo $before_widget;
			if( $title )
				echo $before_title . $title . $after_title;
                
			if( isset($id) && 'bottom-widget-area' != $id ) {
                echo '<div class="widget">';
            }
            
            echo '<ul class="categories' . $cols_class . '">';
			
			foreach( $m[1] as $cat ){
				echo '<li'. ($i++ < $first_count? ' class="first"' : ''). '>'. $cat. '</li>'."\n";
			}
			
			echo '</ul>';
            
            if( isset($id) && 'bottom-widget-area' != $id ) {
                echo '</div>';
            }
            
            if( isset($id) && 'bottom-widget-area' != $id) {
                echo '<div class="widget_b"></div>';
            }
            
			echo $after_widget;
		}
	}
	
	function dt_cats_register() {
		register_widget('DT_cats_Widget');
	}
	
	add_action( 'widgets_init', 'dt_cats_register' );
